﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using IBLL;
using IDAL;
using DI;

namespace BLLMsSql
{
    public abstract class BaseBLL<T>:IBaseBLL<T> where T:class,new()
    {
        //protected IBaseDAL<T> idal = new DALMsSql.BaseDAL<T>();
        protected IBaseDAL<T> idal;
        public abstract void SetIdal();
        public BaseBLL()
        {
            SetIdal();
        }

        //初始化仓储对象
        private IDBSession iDbSession;
        public IDBSession DbSession
        {
            get 
            {
                if (iDbSession == null)
                {
                    iDbSession = null;
                }
                return iDbSession;
            }
        }
        #region 0.数据源
        public IQueryable<T> Entities
        {
            get
            {
                return idal.Entities;
            }
        }
        #endregion

        #region 1.增加
        public int Add(T model)
        {
            return idal.Add(model);
        }
        #endregion

        #region 2.根据ID删除
        public int Del(T model)
        {
            return idal.Del(model);
        }
        #endregion

        #region 3.根据条件删除
        public int DelBy(Expression<Func<T, bool>> delWhere)
        {
            return idal.DelBy(delWhere);
        }
        #endregion

        #region 4.扩展修改方法，（把不需要修改的列用Lambda数组表示出来）
        /// <summary>
        /// 扩展修改方法，（把不需要修改的列用Lambda数组表示出来）
        /// </summary>
        /// <param name="model">要修改的实体</param>
        /// <param name="ignoreProperties">不需要修改的相关字段</param>
        /// <returns>受影响的行数</returns>
        public int Modify(T model, params Expression<Func<T, object>>[] ignoreProperties)
        {
            return idal.Modify(model, ignoreProperties);
        }
        #endregion

        #region 4.修改
        public int Modify(T model, params string[] proNames)
        {
            return idal.Modify(model, proNames);
        }
        #endregion

        #region 5.批量修改

        public int ModifyBy(T model, Expression<Func<T, bool>> whereLambda, params string[] proNames)
        {
            return idal.ModifyBy(model, whereLambda, proNames);
        }
        #endregion

        #region 5.X按照批量修改的列，批量修改

        public int ModifyBy(T model, Expression<Func<T, bool>> whereLambda, Expression<Func<T, object>>[] proNames)
        {
            return idal.ModifyBy(model,whereLambda, proNames);
        }

        #endregion

        #region 6.根据条件查询
        public List<T> GetListBy(Expression<Func<T, bool>> whereLambda)
        {
            return idal.GetListBy(whereLambda);
        }
        #endregion

        #region 7.根据条件排序和查询
        /// <summary>
        /// 根据条件排序和查询
        /// </summary>
        /// <typeparam name="Tkey">排序字段类型</typeparam>
        /// <param name="whereLambda">查询条件</param>
        /// <param name="orderLambda">排序条件</param>
        /// <returns></returns>
        public List<T> GetListBy<Tkey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, Tkey>> orderLambda)
        {
            return idal.GetListBy<Tkey>(whereLambda, orderLambda);
        }

        #endregion

        #region 8.分页查询
        /// <summary>
        /// 根据条件排序分页查询
        /// </summary>
        /// <typeparam name="Tkey">排序字段类型</typeparam>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="whereLambda">查询条件</param>
        /// <param name="orderLambda">排序条件</param>
        /// <returns></returns>
        public List<T> GetPageList<Tkey>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, Expression<Func<T, Tkey>> orderLambda)
        {
            return idal.GetPageList<Tkey>(pageIndex, pageSize, whereLambda, orderLambda);
        }
        #endregion

        #region 9.分页查询总行数
        /// <summary>
        /// 根据条件分页查询总行数
        /// </summary>
        /// <typeparam name="Tkey"></typeparam>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowCount"></param>
        /// <param name="whereLambda"></param>
        /// <param name="orderLambda"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public List<T> GetPageList<Tkey>(int pageIndex, int pageSize, ref int rowCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, Tkey>> orderLambda, bool isAsc = true)
        {
            return idal.GetPageList<Tkey>(pageIndex, pageSize, ref rowCount, whereLambda, orderLambda, isAsc);
        }
        #endregion
    }
}
